﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using vensoft.Entities;
using System.Data;


namespace vensoft.DataAccess
{
    public class HistoricoABDataAccess: MasterDataAcess,IHistoricoABDataAccess
    {
        public HistoricoABDataAccess()
            : base()
        {
           
        }
        public void RegistrarHistoricoAB(HistoricoAB historicoAB)
        {
            SqlCommand commando = base.CreateCommand("RegistrarHistorico_AB");
            AddParameters(historicoAB, commando);
        }

        private void AddParameters(HistoricoAB historicoAB, SqlCommand command)
        {
            base.AddInParameter(command, "ID_Historico_AB", historicoAB.Id_Historico);
            base.AddInParameter(command, "ID_Almacen", historicoAB.ID_Almacen);
            base.AddInParameter(command, "Ubicacion", historicoAB.Ubicacion);
            base.AddInParameter(command, "ID_Prod", historicoAB.ID_Prod);
            base.AddInParameter(command, "Cod_Prod", historicoAB.Cod_Prod);
            base.AddInParameter(command, "Nombre", historicoAB.Nombre);
            base.AddInParameter(command, "Cantidad", historicoAB.Cantidad);
            base.AddInParameter(command, "Movimiento", historicoAB.Movimiento);
            base.AddInParameter(command, "Motivo", historicoAB.Motivo);
            base.AddInParameter(command, "Fecha", historicoAB.Fecha);
            base.AddInParameter(command, "nomb_pers", historicoAB.Nombre_Usuario);
            base.ExecuteCommand(command);

        }
        
        public List<HistoricoAB> GetAllFromHistoricoAB()
        {
            SqlCommand comand = base.CreateSelectCommand("GetAllFromHistoricoAB");
            return SelectObjectFactory(comand);
        }

        private List<HistoricoAB> SelectObjectFactory(SqlCommand comand)
        {
            List<HistoricoAB> lista = new List<HistoricoAB>();
            using (SqlDataReader reader = base.ExecuteReader(comand))
            {
                while (reader.Read())
                {
                    HistoricoAB historicoab = new HistoricoAB()
                    {
                        Id_Historico = reader.GetInt32(0),
                        Ubicacion = reader.GetString(2),
                        Cod_Prod = reader.GetString(4),
                        Nombre = reader.GetString(5),
                        Cantidad = reader.GetInt32(6),
                        Movimiento = reader.GetString(7),                        
                        Motivo = reader.GetString(8),
                        Fecha = reader.GetDateTime(9),
                        Nombre_Usuario = reader.GetString(10)

                    };
                    lista.Add(historicoab);
                }
            }
            return lista;
        }

        public List<HistoricoAB> GetAllFromHistoricoABByFechas(DateTime f_inicio, DateTime f_fin)
        {
            SqlCommand command = base.CreateSelectCommand("GetAllFromHistoricoABByFecha");
            command.Parameters.AddWithValue("f_inicio", f_inicio);
            command.Parameters.AddWithValue("f_fin", f_fin);
            return SelectObjectFactory(command);
        }

        public List<HistoricoAB> GetAllFromHistoricoABByCodProd(string cod_prod)
        {
            SqlCommand command = base.CreateSelectCommand("GetAllFromHistoricoABByCodProd");
            command.Parameters.AddWithValue("cod_prod", cod_prod);
            return SelectObjectFactory(command);
        }
    }
}
